package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.SmsHomeRecommendSubject;
import com.itheima.mapper.SmsHomeRecommendSubjectMapper;
import com.itheima.service.db.SmsHomeRecommendSubjectService;
import com.itheima.util.R;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeRecommendSubjectServiceImpl implements SmsHomeRecommendSubjectService {
    @Autowired
    SmsHomeRecommendSubjectMapper smsHomeRecommendSubjectMapper;

    /**
     * 营销-专题-条件分页查询
     * @param pageNum
     * @param pageSize
     * @param subjectName
     * @param recommendStatus
     * @return
     */
    @Override
    public PageBeanVo findRecommendSubjectByPage(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {
        //分页构造器
        Page<SmsHomeRecommendSubject> page = new Page<>(pageNum, pageSize);

        //条件构造器
        LambdaQueryWrapper<SmsHomeRecommendSubject> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(subjectName),SmsHomeRecommendSubject::getSubjectName,subjectName)
                .eq(recommendStatus != null,SmsHomeRecommendSubject::getRecommendStatus,recommendStatus)
                .orderByDesc(SmsHomeRecommendSubject::getSort);

        //根据mapper查询分页数据
        Page<SmsHomeRecommendSubject> recommendSubjectPage = smsHomeRecommendSubjectMapper.selectPage(page, qw);

        //返回pageBeanVo
        return new PageBeanVo(pageNum,pageSize,recommendSubjectPage.getTotal(),recommendSubjectPage.getRecords());
    }

    /**
     * 营销-专题-添加专题
     * @param smsHomeRecommendSubjectList
     * @return
     */
    @Override
    public R addSubject(List<SmsHomeRecommendSubject> smsHomeRecommendSubjectList) {
        List<SmsHomeRecommendSubject> recommendSubjectList = smsHomeRecommendSubjectMapper.selectList(null);
        //遍历集合,获取每个对象
        for (SmsHomeRecommendSubject smsHomeRecommendSubject : smsHomeRecommendSubjectList) {
            Long subjectId = smsHomeRecommendSubject.getSubjectId();
            for (SmsHomeRecommendSubject homeRecommendSubject : recommendSubjectList) {
                if (subjectId == homeRecommendSubject.getSubjectId()){
                    return R.error(500,"该商品已在推荐列表中");
                }
            }
            smsHomeRecommendSubject.setSort(0);
            smsHomeRecommendSubject.setRecommendStatus(1);
            smsHomeRecommendSubjectMapper.insert(smsHomeRecommendSubject);
        }
        return R.success(null);
    }

    /**
     * 营销-人气-修改首页新品单个|多个推荐状态
     * @param id
     * @param recommendStatus
     */
    @Override
    public void update(Long id, Integer recommendStatus) {
        SmsHomeRecommendSubject smsHomeRecommendSubject = smsHomeRecommendSubjectMapper.selectById(id);
        smsHomeRecommendSubject.setRecommendStatus(recommendStatus);
        smsHomeRecommendSubjectMapper.updateById(smsHomeRecommendSubject);
    }

    /**
     * 营销-人气-删除新品推荐
     * @param id
     */
    @Override
    public void delete(Long id) {
        smsHomeRecommendSubjectMapper.deleteById(id);
    }

    /**
     * 营销-人气-修改首页新品排序
     * @param id
     * @param sort
     */
    @Override
    public void updateSort(Long id, Integer sort) {
        SmsHomeRecommendSubject smsHomeRecommendSubject = smsHomeRecommendSubjectMapper.selectById(id);
        smsHomeRecommendSubject.setSort(sort);
        smsHomeRecommendSubjectMapper.updateById(smsHomeRecommendSubject);
    }
}
